﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class MAS_EduInstitutes : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        private IMasEducationInstituteBLL insBLL = new MasEducationInstituteBLL();
        private IMasEducationDegreeBLL degreeBLL = new MasEducationDegreeBLL();
        private IMasCountryBLL countryBLL = new MasCountryBLL();
        private IMasProvinceBLL provBLL = new MasProvinceBLL();
        private IMasDistrictBLL districtBLL = new MasDistrictBLL();

        private const int ItemsPerRequest = 10;
        private object _dataItem = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                txtPostalCode.Attributes.Add("onkeypress", "return isNumberKey(event)");

                if (DataItem != null)
                {
                    LoadCountry();
                    LoadEduDegree();

                    if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                    {
                        lblSeq.Text = DataBinder.Eval(DataItem, "EDU_INS_SEQ").ToString();
                        txtCode.Text = DataBinder.Eval(DataItem, "EDU_INS_CODE").ToString();
                        txtName.Text = DataBinder.Eval(DataItem, "EDU_INS_NAME").ToString();
                        txtNameEng.Text = DataBinder.Eval(DataItem, "EDU_INS_ENG").ToString();
                        rblInsType.SelectedValue = DataBinder.Eval(DataItem, "EDU_GOV").ToString();
                        txtAddress.Text = DataBinder.Eval(DataItem, "ADDRESS").ToString();
                        txtPostalCode.Text = DataBinder.Eval(DataItem, "POSTAL_CODE").ToString();
                        txtTel.Text = DataBinder.Eval(DataItem, "TEL").ToString();
                        txtFax.Text = DataBinder.Eval(DataItem, "FAX").ToString();
                        cmbCountry.SelectedValue = DataBinder.Eval(DataItem, "COUNTRY_SEQ").ToString();
                        if (cmbCountry.SelectedValue == SystemInfo._Local)
                        {
                            cmbDistrict.Items.Add(
                                new RadComboBoxItem(DataBinder.Eval(DataItem, "DIS_AMP_PROV").ToString(),
                                                    DataBinder.Eval(DataItem, "DISTRICT_SEQ").ToString()));
                            cmbDistrict.SelectedValue = DataBinder.Eval(DataItem, "DISTRICT_SEQ").ToString();
                        }

                        cmbDegree.SelectedValue = DataBinder.Eval(DataItem, "MAX_EDU_DEGREE_SEQ").ToString();
                    }
                    else
                    {
                        cmbCountry.SelectedValue = SystemInfo._Local;
                    }

                    btnUpdate.Visible = !(DataItem is Telerik.Web.UI.GridInsertionObject);
                    btnAddnew.Visible = (DataItem is Telerik.Web.UI.GridInsertionObject);
                }
            }
            catch (Exception err)
            {

            }
        }

        public object DataItem
        {
            get { return this._dataItem; }
            set { this._dataItem = value; }
        }

        private static string GetStatusMessage(int offset, int total)
        {
            if (total <= 0)
                return "ไม่มีข้อมูล";

            return String.Format("รายการที่ <b>1</b>-<b>{0}</b> จากทั้งหมด <b>{1}</b> รายการ", offset, total);
        }

        //protected void LoadDistrict(string Province)
        //{
        //    IList<MasDistrictInfo> infoList = districtBLL.ListDistricts("", "", Province);

        //    cmbDistrict.DataSource = infoList;
        //    cmbDistrict.DataBind();
        //}
        //protected void LoadProvinces()
        //{
        //    IList<MasProvinceInfo> infoList = provBLL.ListMasProvinces();

        //    cmbProvince.DataSource = infoList;
        //    cmbProvince.DataBind();
        //}
        protected void LoadCountry()
        {
            try
            {
                DataSet ds = countryBLL.LoadMasCountries((int?) null, (int?) null);
                DataTable dt = null;
                if (ds.Tables.Count > 0)
                {
                    dt = ds.Tables[0];
                }

                cmbCountry.DataSource = dt;
                cmbCountry.DataBind();
            }
            catch (Exception err)
            {

            }
        }

        protected void LoadEduDegree()
        {
            try
            {
                IList<MasEducationDegreeInfo> infoList = degreeBLL.ListEducationDegree();

                cmbDegree.DataSource = infoList;
                cmbDegree.DataBind();
            }
            catch (Exception err)
            {

            }
        }

        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            insBLL.Insert(txtCode.Text.Trim()
                          , txtName.Text.Trim()
                          , txtNameEng.Text.Trim()
                          , rblInsType.SelectedValue == "1"
                          , txtAddress.Text.Trim()
                          , cmbDistrict.SelectedValue
                          , cmbCountry.SelectedValue
                          , txtPostalCode.Text.Trim()
                          , txtTel.Text.Trim()
                          , txtFax.Text.Trim()
                          , cmbDegree.SelectedValue
                          , ssMng.EmpSeqI);
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            insBLL.Update(Int32.Parse(lblSeq.Text.Trim())
                          , txtCode.Text.Trim()
                          , txtName.Text.Trim()
                          , txtNameEng.Text.Trim()
                          , rblInsType.SelectedValue == "1"
                          , txtAddress.Text.Trim()
                          , cmbDistrict.SelectedValue
                          , cmbCountry.SelectedValue
                          , txtPostalCode.Text.Trim()
                          , txtTel.Text.Trim()
                          , txtFax.Text.Trim()
                          , cmbDegree.SelectedValue
                          , ssMng.EmpSeqI);
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }

        protected void cmbDistrict_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            try
            {
                cmbDistrict.Items.Clear();

                DataSet ds = districtBLL.LoadDistricts((int?) null, (int?) null, (int?) null, e.Text);
                DataTable dt = ds.Tables[0];

                int itemOffset = e.NumberOfItems;
                int endOffset = Math.Min(itemOffset + ItemsPerRequest, dt.Rows.Count);
                e.EndOfItems = (endOffset == dt.Rows.Count);
                int j = 0;
                for (int i = itemOffset; i < endOffset; i++)
                {
                    cmbDistrict.Items.Add(new RadComboBoxItem(dt.Rows[i]["DIS_AMP_PROV"].ToString(),
                                                              dt.Rows[i]["DISTRICT_SEQ"].ToString()));

                    TableRow tr = new TableRow();

                    TableCell tdPosName = new TableCell();
                    Label lblPosName = new Label();
                    lblPosName.Text = dt.Rows[i]["DISTRICT_NAME"].ToString();
                    tdPosName.Width = Unit.Pixel(100);
                    tdPosName.Controls.Add(lblPosName);
                    tr.Cells.Add(tdPosName);

                    TableCell tdClassSpecName = new TableCell();
                    Label lblClassSpecName = new Label();
                    lblClassSpecName.Text = dt.Rows[i]["AMP_NAME"].ToString();
                    tdClassSpecName.Width = Unit.Pixel(100);
                    tdClassSpecName.Controls.Add(lblClassSpecName);
                    tr.Cells.Add(tdClassSpecName);

                    TableCell tdJobName = new TableCell();
                    Label lblJobName = new Label();
                    lblJobName.Text = dt.Rows[i]["PROV_NAME"].ToString();
                    tdJobName.Width = Unit.Pixel(100);
                    tdJobName.Controls.Add(lblJobName);
                    tr.Cells.Add(tdJobName);

                    ((Table) cmbDistrict.Items[j].FindControl("tableDistrict")).Controls.Add(tr);
                    j++;
                }
                e.Message = GetStatusMessage(endOffset, dt.Rows.Count);
            }
            catch (Exception err)
            {

            }
        }
    }
}